<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"/>后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加属性</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <div>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
        <el-form-item label="所属属性模板id" prop="templateId">
          <el-input v-model="ruleForm.templateId"></el-input>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="简介" prop="description">
          <el-input v-model="ruleForm.description"></el-input>
        </el-form-item>
        <el-form-item label="属性类型" prop="type">
          <el-input v-model="ruleForm.type"></el-input>
        </el-form-item>
        <el-form-item label="输入类型" prop="inputType">
          <el-input v-model="ruleForm.inputType"></el-input>
        </el-form-item>
        <el-form-item label="备选值列表" prop="valueList">
          <el-input v-model="ruleForm.valueList"></el-input>
        </el-form-item>
        <el-form-item label="计量单位" prop="unit">
          <el-input v-model="ruleForm.unit"></el-input>
        </el-form-item>
        <el-form-item label="排序序号" prop="sort">
          <el-input v-model="ruleForm.sort"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        templateId: '1',
        name: '测试属性名称01',
        description: '测试属性简介01',
        type: '1',
        inputType: '0',
        valueList: '测试值01,测试值02',
        unit: '测试单位01',
        sort: 99
      },
      rules: {
        templateId: [
          { required: true, message: '请输入所属属性模板id', trigger: 'blur' },
          { min: 1, max: 25, message: '长度在 1 到 25 个字符', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入属性名称', trigger: 'blur' },
          { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入属性简介', trigger: 'blur' },
          { min: 3, max: 25, message: '长度在 4 到 25 个字符', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请输入属性类型', trigger: 'blur' },
          { min: 1, max: 25, message: '长度在 3 到 35 个字符', trigger: 'blur' }
        ],
        inputType: [
          { required: true, message: '请输入输入类型值', trigger: 'blur' },
          { min: 1, max: 5, message: '长度在 1 到 5 个字符', trigger: 'blur' }
        ],
        valueList: [
          { required: true, message: '请输入备选值', trigger: 'blur' },
          { min: 3, max: 35, message: '长度在 3 到 35 个字符', trigger: 'blur' }
        ],
        unit: [
          { required: true, message: '请输入计量单位', trigger: 'blur' },
          { min: 3, max: 35, message: '长度在 3 到 35 个字符', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '请输入排序序号', trigger: 'blur' },
          { pattern:/^(\d{1}|[1-9]{1}[0-9]?)$/, message: '相册排序序号必须是在0~99之间的数字', trigger: 'blur'}
        ],
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9080/attributes/add-new'
          let formData = this.qs.stringify(this.ruleForm);
          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url,formData).then((response)=>{
            let responseBody = response.data;
            if(responseBody.state==20000){
              this.$message.success("添加属性成功!");
              this.resetForm(formName);
            } else{
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>

</style>